﻿// ==========================================================
// File: Universal.WebAPI.Core.HttpMethodOverrideHandler.cs
// Created: 14.12.2014
// Created By: Tim cadenbach
// 
// Copyright (C) 2014 Tim Cadenbach
// License: Apache License 2.0
// ==========================================================
using System.Net.Http;
using System.Threading.Tasks;

namespace Universal.WebAPI.Handlers
{
  public class HttpMethodOverrideHandler : DelegatingHandler
  {
    protected override Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request,
            System.Threading.CancellationToken cancellationToken)
    {
      if (request.Method == HttpMethod.Put)
      {
        request.Method = HttpMethod.Post;
        request.Headers.Add("X-HTTP-Method-Override", "PUT");
      }
      if (request.Method == HttpMethod.Delete)
      {
        request.Method = HttpMethod.Post;
        request.Headers.Add("X-HTTP-Method-Override", "DELETE");
      }
      return base.SendAsync(request, cancellationToken);
    }
  }
}
